﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace AsTekMobile
{
    public partial class frmMain : Form
    {
        public int GuncellemeSuresi { get; set; }
        private bool isWorking = false;
        delegate void SetMontajCallback(object data);
        delegate void SetServisCallback(object data);

        public frmMain(int sure)
        {
            GuncellemeSuresi = sure;
            InitializeComponent();
            this.Load += new EventHandler(frmMain_Load);
            this.Activated += new EventHandler(frmMain_Activated);
            this.Deactivate += new EventHandler(frmMain_Deactivate);
        }

        void frmMain_Deactivate(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        void frmMain_Activated(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            grdServisler.AllowEditing = false;
            grdMontajlar.AllowEditing = false;
            Status();
            beginDataRefresh(Guncellenecek.Tumu);
            timer1.Interval = GuncellemeSuresi * 60 * 1000;
            timer1.Enabled = true;
        }

        private void SetMontajDataSource(object montaj)
        {
            if (this.grdMontajlar.InvokeRequired)
            {
                SetMontajCallback d = new SetMontajCallback(SetMontajDataSource);
                this.Invoke(d, new object[] { montaj });
            }
            else
            {
                grdMontajlar.DataSource = montaj;
                for (int i = 0; i < grdMontajlar.Cols.Count; i++)
                {
                    grdMontajlar.Cols[i].AllowEditing = false;
                    if (i > 1 && i < 6)
                    { }
                    else
                        grdMontajlar.Cols[i].Visible = false;
                }
                grdMontajlar.Cols[4].Caption = "Ana Cari";
                grdMontajlar.AutoSizeCols();
            }
        }

        private void SetServisDataSource(object servis)
        {
            if (this.grdServisler.InvokeRequired)
            {
                SetServisCallback d = new SetServisCallback(SetServisDataSource);
                this.Invoke(d, new object[] { servis });
            }
            else
            {
                grdServisler.DataSource = servis;
                for (int i = 0; i < grdServisler.Cols.Count; i++)
                {
                    grdServisler.Cols[i].AllowEditing = false;
                    if (i > 5)
                    {
                        grdServisler.Cols[i].Visible = false;
                    }
                }
                grdServisler.Cols[0].Visible = false;
                grdServisler.Cols[1].Caption = "Tarih";
                grdServisler.Cols[2].Caption = "Sorunlar";
                grdServisler.Cols[4].Caption = "Ana Cari";
                grdServisler.AutoSizeCols();
            }
        }

        private enum Guncellenecek
        {
            Montaj,
            Servis,
            Tumu
        }
        private void beginDataRefresh(Guncellenecek tip)
        {
            try
            {
                switch (tip)
                {
                    case Guncellenecek.Montaj:
                        Hafiza.Ctor.Remove("Montajlar");
                        SetMontajDataSource(Database.ctor.Montajlar);
                        break;
                    case Guncellenecek.Servis:
                        Hafiza.Ctor.Remove("Servisler");
                        SetServisDataSource(Database.ctor.Servisler);
                        break;
                    case Guncellenecek.Tumu:
                        Hafiza.Ctor.Remove("Servisler");
                        Hafiza.Ctor.Remove("Montajlar");
                        SetMontajDataSource(Database.ctor.Montajlar);
                        SetServisDataSource(Database.ctor.Servisler);
                        break;
                }
            }
            catch (Exception ex)
            {
                ErrorMgr.Opps(ex);
            }
        }


        private void menuItem2_Click(object sender, EventArgs e)
        {
            frmSevk frm = new frmSevk();
            frm.EvrakTipi = EvrakTipleri.DepoSevkGiris;
            frm.ShowDialog();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            frmSevk frm = new frmSevk();
            frm.EvrakTipi = EvrakTipleri.DepoSevkCikis;
            frm.ShowDialog();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            MontajSec();
        }

        private void MontajSec()
        {
            try
            {
                if (grdMontajlar.Row > -1)
                {
                    int id = Cevir.Parse<int>(grdMontajlar[grdMontajlar.Row, 1]);
                    using (frmMontaj frm = new frmMontaj())
                    {
                        frm.SecilenMontaj = Database.ctor.Montajlar.Where(x => x.ID == id).FirstOrDefault();
                        frm.ShowDialog();
                        beginDataRefresh(Guncellenecek.Montaj);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorMgr.Opps(ex);
            }
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            ServisSec();
        }

        private void ServisSec()
        {
            try
            {
                if (grdServisler.Row > -1)
                {
                    int id = Cevir.Parse<int>(grdServisler[grdServisler.Row, 7]);
                    using (frmServisMain frm = new frmServisMain())
                    {
                        frm.SecilenServis = Database.ctor.Servisler.Where(x => x.ID == id).FirstOrDefault();
                        frm.ShowDialog();
                        beginDataRefresh(Guncellenecek.Servis);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorMgr.Opps(ex);
            }
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            beginDataRefresh(Guncellenecek.Tumu);
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                isWorking = true;
                beginDataRefresh(Guncellenecek.Tumu);
            }
            catch (Exception)
            {
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            isWorking = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                Status();

                if (!isWorking)
                {
                    backgroundWorker1.WorkerSupportsCancellation = true;
                    backgroundWorker1.WorkerReportsProgress = false;
                    backgroundWorker1.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                ErrorMgr.Opps(ex);
            }
        }

        private void Status()
        {
            label3.Text = "Durum:" + Database.ctor.State();
            if (label3.Text.IndexOf("Online") > -1)
                label3.ForeColor = Color.Green;
            else
                label3.ForeColor = Color.Red;
        }

        private void grdServisler_DoubleClick(object sender, EventArgs e)
        {
            ServisSec();
        }

        private void grdMontajlar_DoubleClick(object sender, EventArgs e)
        {
            MontajSec();
        }

        private void txtServis_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtServis.Text = Convert.ToInt32(txtServis.Text).ToString();
                Service srv = Database.ctor.Servisler.Where(x => x.AracKodu == txtServis.Text).FirstOrDefault();
                if (srv != null)
                {
                    using (frmServisMain frm = new frmServisMain())
                    {
                        frm.SecilenServis = srv;
                        frm.ShowDialog();
                        beginDataRefresh(Guncellenecek.Servis);
                        txtServis.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("Servis Kaydı bulunamadı !");
                }

            }
        }

        private void txtMontaj_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtMontaj.Text = Convert.ToInt32(txtMontaj.Text).ToString();
                Montaj mn = Database.ctor.Montajlar.Where(x => x.AracKodu == txtMontaj.Text).FirstOrDefault();
                if (mn != null)
                {
                    using (frmMontaj frm = new frmMontaj())
                    {
                        frm.SecilenMontaj = mn;
                        frm.ShowDialog();
                        beginDataRefresh(Guncellenecek.Montaj);
                        txtMontaj.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("Montaj Kaydı bulunamadı !");
                }
            }
        }
    }
}